#include <iostream>
#include <string>
#include <mysql/mysql.h>

#define HOST "127.0.0.1"
#define USER "root"
#define PASSWD "1257"
#define DBNAME "learning"
#define PORT 3306
int main() {
    MYSQL *mysql = mysql_init(nullptr);
    if (mysql == nullptr) {
        std::cout << "MySQL init failed\n";
        return -1;
    }

    if (mysql_real_connect(mysql, HOST, USER, PASSWD, DBNAME, PORT, nullptr, 0) == nullptr) {
        std::cout << "MySQL connect failed: " << mysql_error(mysql) << std::endl;
        return -1;
    }

    if (mysql_set_character_set(mysql, "utf8") != 0) {
        std::cout << "charset set failed: " << mysql_error(mysql) << std::endl;
        return -1;
    }

    // 数据库选择已完成, 此处跳过

    // ======================== 插入 ====================================================
    const char *insert1 = "insert into `student` values (NULL, '张三', 18, 13, 82, 43);";
    const char *insert2 = "insert into `student` values (NULL, '李四', 18, 16, 73, 18);";
    const char *insert3 = "insert into `student` values (NULL, '王五', 18, 51, 45, 83);";
    if (mysql_query(mysql, insert1) != 0) {
        std::cout << "sql: " << insert1 << std::endl;
        std::cout << "error reason: " << mysql_error(mysql) << std::endl;
        mysql_close(mysql);
        return -1;
    }
    if (mysql_query(mysql, insert2) != 0) {
        std::cout << "sql: " << insert2 << std::endl;
        std::cout << "error reason: " << mysql_error(mysql) << std::endl;
        mysql_close(mysql);
        return -1;
    }
    if (mysql_query(mysql, insert3) != 0) {
        std::cout << "sql: " << insert3 << std::endl;
        std::cout << "error reason: " << mysql_error(mysql) << std::endl;
        mysql_close(mysql);
        return -1;
    }

    // ======================== 修改 ====================================================
    const char *update1 = "update student set Chinese=Chinese+20 where name='张三';";
    const char *update2 = "update student set age=20 where name='李四';";
    const char *update3 = "update student set name='赵六' where name='王五';";
    if (mysql_query(mysql, update1) != 0) {
        std::cout << "sql: " << update1 << std::endl;
        std::cout << "error reason: " << mysql_error(mysql) << std::endl;
        mysql_close(mysql);
        return -1;
    }
    if (mysql_query(mysql, update2) != 0) {
        std::cout << "sql: " << update2 << std::endl;
        std::cout << "error reason: " << mysql_error(mysql) << std::endl;
        mysql_close(mysql);
        return -1;
    }
    if (mysql_query(mysql, update3) != 0) {
        std::cout << "sql: " << update3 << std::endl;
        std::cout << "error reason: " << mysql_error(mysql) << std::endl;
        mysql_close(mysql);
        return -1;
    }

    // ======================== 删除 ====================================================
    // const char *remove = "delete from student;";
    // if (mysql_query(mysql, remove) != 0) {
    //     std::cout << "sql: " << remove << std::endl;
    //     std::cout << "error reason: " << mysql_error(mysql) << std::endl;
    //     mysql_close(mysql);
    //     return -1;
    // }
    
    // ======================== 查询 ====================================================
    const char *select = "select * from `student`;";
    if (mysql_query(mysql, select) != 0) {
        std::cout << "sql: " << select << std::endl;
        std::cout << "error reason: " << mysql_error(mysql) << std::endl;
        mysql_close(mysql);
        return -1;
    }

    MYSQL_RES *result = mysql_store_result(mysql);
    if (result == nullptr) {
        mysql_close(mysql);
        return -1;
    }

    uint64_t row_size = mysql_num_rows(result);
    unsigned int col_size = mysql_num_fields(result);
    for (int i = 0; i < row_size; i++) {
        MYSQL_ROW row = mysql_fetch_row(result);    // 这个获取其实类似于row = arr++, arr是数组首地址
        for (int j = 0; j < col_size; j++) {
            std::cout << row[j] << "\t";
        }
        std::cout << std::endl;
    }
    mysql_free_result(result);
    mysql_close(mysql);
    return 0;
}
